// pages/shop/car/car.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    carts: [
      { id: '1',  num: '1', price: '198.0', selected: false },
      { id: '2',  num: '1', price: '100.0', selected: false },
      { id: '3',  num: '3', price: '15.0', selected: false },
      { id: '4',  num: '2', price: '15.2', selected: false },
      { id: '5',  num: '1', price: '122.0', selected: false },
    ],
    handel:'order',
    msg:"",
    btn_text:'去结算',
    edit_text:"编辑",
    selectedAllStatus:false,
    selected:[],
    count:0,
    price:0.00,
    checkNull:false,
    bgshow:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 获取组件
    this.dialog = this.selectComponent('#msg');
  },
  // 组件中的方法
  showDialog() {
    this.dialog.showDialog();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  checkboxChange:function(e){
    var length = e.detail.value.length,selectedAllStatus = '',selected = e.detail.value;
    if (length == this.data.carts.length){
      selectedAllStatus = true;
    }else{
      selectedAllStatus = false
    }
    this.setData({
      selectedAllStatus: selectedAllStatus,
      selected: selected
    })
  },
  // 全选
  selectAll:function(){
    var carts = this.data.carts, 
        num = 0, 
        numAll = parseInt(this.data.count),
        price = 0,
        priceAll = parseFloat(this.data.price),
        arr = new Array(),
        selectedAllStatus = !this.data.selectedAllStatus;
    for(var i = 0;i<carts.length;i++){
      carts[i].selected = selectedAllStatus;
      num = num + parseInt(this.data.carts[i].num), price = price + parseFloat(this.data.carts[i].price) * parseInt(this.data.carts[i].num);
      if(selectedAllStatus){
        arr.push(carts[i].id);
      }
    }
    if (selectedAllStatus) {
      num = num;
      price = price;
    } else {
      num = numAll - num;
      price = priceAll - price;
    }
    this.setData({
      selectedAllStatus: selectedAllStatus,
      price: price,
      count: num,
      carts:carts,
      selected:arr
    })
  },
  checkOne:function(e){
    var index = e.currentTarget.dataset.index,
        carts = this.data.carts,
        count = parseInt(this.data.count),
        price = parseFloat(this.data.price),
        cart = carts[index];
        cart.selected = !cart.selected;
        if (!cart.selected){
          console.log(parseFloat(cart.price) * parseInt(cart.num))
          console.log(price)
          price = (price - parseFloat(cart.price) * parseInt(cart.num)).toFixed(2);
          count = count - parseInt(cart.num)
          console.log(count,price)
        }else{
          price = (price + parseFloat(cart.price) * parseInt(cart.num)).toFixed(2);
          count = count + parseInt(cart.num)
        }
    this.setData({
      carts:carts,
      price:price,
      count:count
    })
  },
  // 删除操作
  checkDel:function(e){
    var text = e.currentTarget.dataset.text, btn_text = '', edit_text = '', handel = '';
    if(text == "完成"){
      btn_text = "去结算";
      edit_text= "编辑";
    }else{
      btn_text = "删除";
      edit_text= "完成";
      handel = "del";
    }
    this.setData({
      btn_text: btn_text,
      edit_text: edit_text,
      handel: handel
    })
  },
  order:function(){
    wx.navigateTo({
      url: '../buy/buy',
    })
  },
  del:function(){
    var _this = this,_data = this.data,
       length = _data.selected.length,
       carts = _data.carts,
       count = 0,price = 0.00
    if (length <= 0){
      this.msgContent("请选择要删除的商品");return false;
    }
    wx.showModal({
      title: '提示',
      content: '是否删除选中的商品',
      success: function(res) {
      if (res.confirm) {
        for (var i = 0; i < length;i++){
          carts.splice(carts[i], 1)
        }
        
        if (length == _data.carts.length){
          count = 0;
          price = 0.00
        }
        var bgshow = _this.checkNull(_data.carts);//背景提示是否显示
        console.log(bgshow)
        _this.setData({
          carts:carts,
          count:count,
          price: price, 
          selectedAllStatus:false,
          selected:[],
          bgshow:bgshow,
          bgmsg: '没有记录',
          bgimg: '../../asset/image/icon/order.png'
        })
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
     
    }
    })
  },
    /**消息提示 */
  msgContent: function (val) {
    this.setData({
      msg: val
    })
    this.showDialog();
  },
  // 购物车是否为空
  checkNull:function(val){
    var len = val.length,bool = '';
    if(len){
      bool = false;
    }else{
      bool = true;
    }
    return bool;
  },
  // 是否为最小值1
  minVal:function(){
    
  }
})